package review.system.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import review.config.Constants;
import review.entity.Department;
import review.system.service.DepartmentService;

@Controller
@RequestMapping("/syatem")
@Api(tags = {"部门操作接口"})
public class DepartmentAction {
	@Autowired
	private DepartmentService departmentService;
	
	@ApiOperation(value = "添加部门", notes = "", httpMethod = "POST")
  @RequestMapping(value="/add_dept",produces = "application/json;charset=UTF-8")
  @ResponseBody
	public Object add(Department dept) {
		JSONObject obj = new JSONObject();
		try {
			departmentService.addDepartment(dept);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, dept);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}
	
	@ApiOperation(value = "分页查询部门", notes = "", httpMethod = "GET")
  @RequestMapping(value="/query_dept",produces = "application/json;charset=UTF-8")
  @ResponseBody
	public Object query(Department dept,
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int size) {
		JSONObject obj = new JSONObject();
		try {
			Page<Department> depts = departmentService.findAll(page, size, dept);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, depts);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

}
